﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Newtor.VoteMan.Providers
{
    public class LinqUsersOpinionManager : IUsersOpinionManager
    {

        #region IUsersOpinionManager 成员

        public bool HasUserVoted(string userName, Guid voId)
        {
            using (VoteDataContext vdc = new VoteDataContext())
            {
                return (from uo in vdc.UsersOpinion
                        where uo.UserName == userName && uo.OpinionId == voId
                        select uo).Count() > 0;
            }
        }

        public int GetBallot(Guid voId)
        {
            using (VoteDataContext vdc = new VoteDataContext())
            {
                return (from uo in vdc.UsersOpinion
                        where uo.OpinionId == voId
                        select uo).Count();
            }
        }


        public bool HasUserVoted(string userName, VoteItem vi)
        {
            using (VoteDataContext vdc = new VoteDataContext())
            {
                var vos = from vo in vdc.VoteOpinion
                          where vo.ItemId == vi.ItemId
                          select vo;
                int a = vos.Count();

                var votedOpinions = (from uo in vdc.UsersOpinion
                        where uo.UserName == userName
                        select uo.Opinion);

                a = votedOpinions.Count();

                return votedOpinions.ToList().Exists(delegate(VoteOpinion vo)
                {
                    return vos.Contains(vo);
                });
            }
        }

        public void Poll(string userName, Guid voId)
        {
            UsersOpinion uo = new UsersOpinion
            {
                UserName = userName,
                OpinionId = voId
            };
            using (VoteDataContext vdc = new VoteDataContext())
            {
                vdc.UsersOpinion.InsertOnSubmit(uo);
                vdc.SubmitChanges();
            }
        }


        public VoteOpinion GetUsersOpinion(VoteItem item, string userName)
        {
            using (VoteDataContext vdc = new VoteDataContext())
            {
                try
                {
                    return (from vo in vdc.VoteOpinion
                            from uo in vdc.UsersOpinion
                            where (vo.ItemId == item.ItemId
                                    && uo.OpinionId == vo.OpinionId
                                    && uo.UserName == userName
                                    )
                            select vo).First();
                }
                catch (InvalidOperationException)
                {
                    return null;
                }
            }
        }


        public VoteOpinion GetBestOpinion(VoteItem item)
        {
            using (VoteDataContext vdc = new VoteDataContext())
            {
                var ops = from vo in vdc.VoteOpinion
                          where vo.ItemId == item.ItemId
                          select vo;
                int maxballot = -1;
                VoteOpinion result = null;
                foreach (var op in ops)
                {
                    int opBallot = (from uo in vdc.UsersOpinion
                               where uo.OpinionId == op.OpinionId
                               select uo).Count();
                    if (opBallot > maxballot)
                    {
                        maxballot = opBallot;
                        result = op;
                    }
                }
                return result;
            }
        }

        #endregion
    }
}
